package com.sc.web.gwt.client.view.agentwidget.contents;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.sc.web.gwt.client.service.AgentAdminDataManagementService;
import com.sc.web.gwt.client.service.AgentAdminDataManagementServiceAsync;
import com.sc.web.gwt.client.view.uicomponent.ValidatablePasswordTextBox;
import com.sc.web.gwt.client.view.uicomponent.ValidatableTextBox;
import com.sc.web.gwt.shared.model.AgentAdmin;

public class AgentAccountManagementWidget extends Composite {
    private static Binder binder = GWT.create(Binder.class);

    AgentAdminDataManagementServiceAsync agentAdminDataManagementServiceAsync = GWT.create(AgentAdminDataManagementService.class);

    private final AgentAccountManagementWidget thisWidget = this;
    private AgentAdmin agent = null;
    @UiField
    ValidatablePasswordTextBox newpwd;
    @UiField
    ValidatablePasswordTextBox oldpwd;
    @UiField
    ValidatablePasswordTextBox repwd;
    @UiField
    Button buttonClear;
    @UiField
    Button buttonSubmit;
    @UiField
    ValidatableTextBox company;
    @UiField
    ValidatableTextBox email;
    @UiField
    ValidatableTextBox phone;
    @UiField
    ValidatableTextBox position;
    @UiField
    ValidatableTextBox sitename;
    @UiField
    ValidatableTextBox username;
    @UiField
    ValidatableTextBox agentName;

    public interface Binder extends UiBinder<HTMLPanel, AgentAccountManagementWidget> {
    }


    public AgentAccountManagementWidget() {
        initWidget(binder.createAndBindUi(this));


        agentAdminDataManagementServiceAsync.getAgentAdminInfo(new AsyncCallback<AgentAdmin>() {
            public void onFailure(Throwable throwable) {
                throwable.printStackTrace();
            }

            public void onSuccess(AgentAdmin result) {
                if (result != null) {
                    agent = result;
                    buttonSubmit.addClickHandler(new SubmitHandler());
                    buttonClear.addClickHandler(new ClearHandler());
                    thisWidget.reset();
                } else Window.alert("A problem occurred. Please reload the page");
            }
        });
    }

    private void reset() {
        if (agent != null) {
            username.setText(agent.getAgentAdminAccount());
            agentName.setText(agent.getAgentAdminName());
            email.setText(agent.getAgentAdminEmail());
            phone.setText(agent.getAgentAdminPhoneNumber());
            company.setText(agent.getAgentAdminCompany());
            sitename.setText(agent.getSiteName());
            position.setText(agent.getAgentAdminPosition());
            oldpwd.setText("");
            newpwd.setText("");
            repwd.setText("");
            oldpwd.clearStatus();
            newpwd.clearStatus();
            repwd.clearStatus();
        }
    }

    private class SubmitHandler implements ClickHandler {
        public void onClick(ClickEvent clickEvent) {
            boolean isValid = true;
            if (oldpwd.getText().toString().compareTo("") == 0) {
                oldpwd.showError("Please enter old password.");
                isValid = false;
            }
            if (newpwd.getText().toString().compareTo("") == 0) {
                newpwd.showError("Please enter new password.");
                isValid = false;
            }
            if (repwd.getText().toString().compareTo("") == 0) {
                repwd.showError("Please re-enter new password.");
                isValid = false;
            }
            if (isValid) {
                if (oldpwd.getText().toString().compareTo(agent.getAgentAdminPassword()) != 0) {
                    oldpwd.showError("Incorrect password.");
                } else if (newpwd.getText().toString().compareTo(repwd.getText().toString()) != 0) {
                    repwd.showError("Passwords don't match.");
                } else {
                    agent.setAgentAdminPassword(newpwd.getText().toString());
                    agentAdminDataManagementServiceAsync.saveOrUpdateAgentAdminData(agent, new AsyncCallback<Boolean>() {
                        public void onFailure(Throwable throwable) {
                            throwable.printStackTrace();
                        }

                        public void onSuccess(Boolean result) {
                            if (result) {
                                Window.alert("Information updated successfully");
                            } else {
                                Window.alert("A problem occurred, please try again.");
                            }
                        }
                    });
                }
            }
        }
    }

    private class ClearHandler implements ClickHandler {
        public void onClick(ClickEvent clickEvent) {
            thisWidget.reset();
        }
    }
}